﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Synchronization.BusinessLogic;
using Sustainalytics.Synchronization.Interface;
using Sustainalytics.ReportGenerator.BL.Tests;

namespace Sustainalytics.Synchronization.Service.Tests
{
    /// <summary>
    /// Summary description for HistoricalDataSyncStepTests
    /// </summary>
    [TestClass]
    public class HistoricalDataSyncStepTests
    {
        private const DatabaseGroup CurrentDatabaseGroup = DatabaseGroup.B;
        private const string IpAddress = "54.154.132.43";

        private static readonly string ClientDwConnectionString = string.Format("mongodb://{0}:27017/ClientDW_{1}", IpAddress, CurrentDatabaseGroup);
        private static readonly string DwArchiveConnectionString = string.Format("mongodb://{0}:27017/DWArchive_{1}", IpAddress, CurrentDatabaseGroup);
        private static readonly string RPHistoricalConnectionString = string.Format("mongodb://{0}:27017/RPHistoricalDataSet", IpAddress);
        private static readonly string ClientPlatform = string.Format("mongodb://{0}:27017/ClientPlatform", IpAddress);

        private HistoricalDataSyncStep sutSyncStep;
        [TestMethod]
        public void StartTest()
        {
            this.sutSyncStep = new HistoricalDataSyncStep();

            var syncContext = TestSyncContextFactoy.Create();

            this.sutSyncStep.Start(syncContext);
        }


        [TestMethod]
        public void ShouldExecuteSync_When_LastSync_FirstDayOfCurrentMonth_Returns_False()
        {
            DateTime storingDate, processingDate;
            var currentDate = new DateTime(2017,09, 23, 0, 0, 0, DateTimeKind.Utc);
            var actual = HistoricalDataSyncStep.ShouldExecuteSync(false, currentDate, new DateTime(2017, 09, 01, 0, 0, 0, DateTimeKind.Utc), out processingDate,
                out storingDate);
            Assert.IsFalse(actual);
        }


        [TestMethod]
        public void ShouldExecuteSync_When_CurrentDate_FirstDayOfYear_Returns_True()
        {
            DateTime storingDate, processingDate;
            var currentDate = new DateTime(2017, 01, 31, 0, 0, 0, DateTimeKind.Utc);
            var actual = HistoricalDataSyncStep.ShouldExecuteSync(false, currentDate, new DateTime(2016, 12, 01, 0, 0, 0, DateTimeKind.Utc), out processingDate,
                out storingDate);
            Assert.IsTrue(actual);
        }

        [TestMethod]
        public void ShouldExecuteSync_When_CurrentDate_LastDayOfMonth_Returns_True()
        {
            DateTime storingDate, processingDate;
            var currentDate = new DateTime(2017, 03, 31, 0, 0, 0, DateTimeKind.Utc);
            var actual = HistoricalDataSyncStep.ShouldExecuteSync(false, currentDate, default(DateTime), out processingDate,
                out storingDate);
            Assert.IsTrue(actual);
        }


        [TestMethod]
        public void ShouldExecuteSync_When_No_Historical_date_for_current_month()
        {
            DateTime storingDate, processingDate;
            var currentDate = new DateTime(2016, 02, 09, 0, 0, 0, DateTimeKind.Utc);
            var actual = HistoricalDataSyncStep.ShouldExecuteSync(false, currentDate, DateTime.Today.AddMonths(-1), out processingDate,
                out storingDate);
            Assert.IsTrue(actual);
        }

        [TestMethod]
        public void ShouldExecuteSync_When_Forced_Sync_Returns_True()
        {
            DateTime storingDate, processingDate;
            var currentDate = new DateTime(2015, 8, 31, 0, 0, 0, DateTimeKind.Utc);
            var actual = HistoricalDataSyncStep.ShouldExecuteSync(true, currentDate, default(DateTime), out processingDate,
                out storingDate);
            Assert.IsTrue(actual);
        }


        [TestMethod]
        public void GetDatabaseInfo_should_return_a_setId()
        {
            this.sutSyncStep = new HistoricalDataSyncStep();

            var syncContext = new SyncContext(ErrorPolicy.CarryOn)
            {
                GAConnectionStrings = new Dictionary<string, string>
                {
                    {"ClientDW", ClientDwConnectionString },
                    {"DWArchive", DwArchiveConnectionString },
                    {"RPHistoricalDataSet", RPHistoricalConnectionString },
                    {"ClientPlatform", ClientPlatform }
                },
                LastSyncTime = new DateTime(2017, 03, 31, 0, 0, 0, DateTimeKind.Utc),
                Report = new ConcurrentBag<string>()
            };

            this.sutSyncStep.Start(syncContext);
            var actual = this.sutSyncStep.GetLastAvailableEsgComputationInfo();
            Assert.IsTrue(actual != null);
        }
    }
}
